<!DOCTYPE html>
<html>

<head>
  <title>WebGPU caching test</title>
  <style type="text/css">
    .nomargin {
      margin: 0px auto;
    }
  </style>

  <script type="module">
    import { webGpuUnitTests } from './webgpu-unittest-utils.js';

    function finish(success, errors) {
        if (!success) {
            console.error(errors.join('\n'));
            errors.forEach(e => domAutomationController.log(e));
            domAutomationController.send('FAILED');
        } else {
            console.log('SUCCESS');
            domAutomationController.send('SUCCESS');
        }
    }

    async function main() {
        const parsedString = new URLSearchParams(window.location.search);

        // Get the test parameters.
        const testId = parsedString.get('testId');
        const hostname = parsedString.get('hostname');
        const worker_string = parsedString.get('worker');
        const use_worker =
              (worker_string != null) ? worker_string == 'true' : false;
        const runs_string = parsedString.get('runs');
        const runs = (runs_string != null) ? parseInt(runs_string) : 1;
        const iframe_string = parsedString.get('iframe');
        const is_iframe =
              (iframe_string != null) ? iframe_string == 'true' : false;

        // If we pass an explicit hostname, then forward the parameters inside
        // an iframe at the hostname.
        if (hostname != null) {
            parsedString.delete('hostname');
            parsedString.set('iframe', 'true');
            const iframe = document.createElement('iframe');
            const path = window.location.pathname;
            const search = parsedString.toString();
            const port = window.location.port;
            iframe.src = `http://${hostname}:${port}${path}?${search}`;
            document.body.append(iframe);

            const [success, errors] = await (new Promise(
                resolve => {
                    window.addEventListener('message',
                                            e => {
                                                resolve(e.data);
                                                },
                                            { once: true })
                }
            ));
            finish(success, errors);
            return;
        }

        var success = true;
        var errors = [];
        for (let i = 0; i < runs; i++) {
            if (!use_worker) {
                const [run_success, run_errors] =
                      await webGpuUnitTests.runTest(testId);
                success = success && run_success;
                errors = errors.concat(run_errors);
            } else {
                const worker = new Worker(
                    './webgpu-unittest-worker.js',
                    { type: 'module' }
                );
                const promise = new Promise(
                    resolve => {
                        worker.addEventListener('message',
                                                e => {
                                                    resolve(e.data);
                                                },
                                                { once: true })
                    }
                );
                worker.postMessage({ testId });
                const [run_success, run_errors] = await promise;
                success = success && run_success;
                errors = errors.concat(run_errors);
            }
        }

        if (is_iframe) {
            // In this case, we are an iframe embedded inside a parent, so
            // post the results back to the parent to be handled.
            parent.postMessage([success, errors], '*');
        } else {
            finish(success, errors);
        }
    }

    await main();
  </script>
</head>

<body>
</body>

</html>
